<?php

function getTableFile($table) {
    $commonTable = array("accounts", "area", "ads");
    $ordTable = array("play", "service","dictionary");
    if (in_array($table, $commonTable)) {
        $ord = "ord ASC";
        $where = "is_check=1";
    } elseif (in_array($table, $ordTable)) {
        $ord = "ord ASC,id DESC";
    } elseif ($table == 'gifts') {
        $where = "is_mall=1";
        $ord = "ord ASC";
    }elseif ($table == 'award') {
        $where = "is_award=1";
        $ord = "ord ASC";
       
    } elseif ($table == 'admin_menu') {
        $ord = "ord ASC";
    }
    $lists = F('' . $table . '/data');
    if (empty($lists)) {
        
        if ($table == 'award') {
             $table = 'gifts';
        }
        $lists = M($table)->where($where)->order($ord)->select();
      
        F('' . $table . '/data', $lists);
    }
    return $lists;
}
function getDictionary($code, $type = 0) {
    $lists = getTableFile('dictionary');
    $arr = array();
    foreach ($lists as $k => $v) {
        if ($v['code'] == $code) {
            if ($type == -1) {
                $arr[] = $v;
            } else {
                if ($v['pid'] == $type) {
                    $arr[] = $v;
                }
            }
        }
    }
    return $arr;
}

function getTableConfig() {
    $config = F('config/data');
    if (empty($config)) {
        $config = M("config")->where("id = 1")->find();
        F('config/data', $config);
    }
    return $config;
}

function getTableField($field, $table) {
    $rs = F($field);
    if (empty($rs)) {
        $info = M($table)->field($field)->where("id = 1")->find();
        $rs = $info[$field];
    }
    return $rs;
}

function getAdsCat($pid, $field) {
    $lists = F('adsCat/data');
    if (empty($lists)) {
        $lists = M("ads_cat")->where("mtype = 1")->order("ord ASC")->select();
        // echo $Dao->getLastSql();
        F('adsCat/data', $lists);
    }
    if ($pid > 0) {
        foreach ($lists as $v) {
            if ($v['id'] == $pid) {
                return $v[$field];
                break;
            }
        }
    } else {
        return $lists;
    }
}

function getTableFileLists($table, $pid) {
    $lists = getTableFile($table, 1);
    // print_r($lists);
    foreach ($lists as $k => $v) {
        if ($v['pid'] == $pid) {
            $arr[$k] = $v;
        }
    }
    return $arr;
}

function getSub($table, $id) {
    $lists = getTableFile($table);
    $arr = array();
    foreach ($lists as $k => $v) {
        if ($v['pid'] == $id) {
            $arr[] = $v;
        }
    }
    return $arr;
}

function getSubField($table, $id, $field) {
    $lists = getTableFile($table);
    $arr = array();
    if($lists){
        foreach ($lists as $k => $v) {
        if ($v['id'] == $id) {
            $arr = $v;
            break;
        }
    }
    if ($field != '') {
        return $arr[$field];
    } else {
        return $arr;
    }
    }
    
}

?>
